// pages/barcode/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: '',
    urls:[],
    loadModal:false,//显示加载框
    websitename:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.createBar();
    this.setData({
      data:wx.getStorageSync('account'),
      websitename:wx.getStorageSync('websitename')
    })
  },
  createBar: function () {
    if (this.data.data == '') {
      wx.showToast({
        title: '结构号不能为空',
      })
      return;
    }
    var url = wx.getStorageSync('bar'+wx.getStorageSync('account'));
    if (url!='') {
      console.log("从本地获取数据");
      var urls = [];
      urls.push(url);
      this.setData({
        urls:urls
      })
      return;
    }
    var that = this;
    var path = 'pages/register/index'
    var width = '430';
    this.showload(true);
    wx.cloud.callFunction({
      name: 'createbar',
      data: {
        page: path,
        width: width,
        scene: 'websitecode='+wx.getStorageSync('account'),
        wdbm:wx.getStorageSync('account')
      },
      success: res => {
        console.log(res)
        var result = res.result;
        var url = [];
        that.showload(false);
        if(result.code =="1001") {
          wx.showToast({
            title: '生成二维码失败',
          })
        } else {
          url.push(result.url);
          wx.setStorageSync('bar'+wx.getStorageSync('account'), result.url);
          this.setData({
              urls:url
          })
        }
        
      },
      fail: error => {
        that.showload(false);
        console.log(JSON.stringify(error))
        wx.showToast({
          title: '生成失败!',
        })
      }
    });
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.urls // 需要预览的图片http链接列表  
    })
  } ,   
  showload:function(stats){
    this.setData({
      loadModal:stats
    })
  } 
})